package cn.dubito.o2o.web.shop;

import cn.dubito.o2o.domian.dto.ShopExecution;
import cn.dubito.o2o.domian.entity.Area;
import cn.dubito.o2o.domian.entity.Shop;
import cn.dubito.o2o.domian.entity.ShopCategory;
import cn.dubito.o2o.domian.entity.User;
import cn.dubito.o2o.service.ShopService;
import cn.dubito.o2o.utils.RequestUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author liliang
 * @description:
 * @date 2018/4/24.
 */
@Controller
@RequestMapping("/shop")
public class ShopManagementController {

    @Autowired
    private ShopService shopService;

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String page() {
        return "shop/shopoperation";
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    @ResponseBody
    private Map<String, Object> registerShop(HttpServletRequest request, @RequestParam(value = "shopImg", required = false) MultipartFile shopImg) throws IOException {
        Map<String, Object> model = new HashMap<>(16);
        ServletInputStream inputStream = request.getInputStream();
        String shopStr = RequestUtil.getString(request, "shop");
        ObjectMapper mapper = new ObjectMapper();
        Shop shop = null;
        try {
            shop = mapper.readValue(shopStr, Shop.class);
        } catch (IOException e) {
            model.put("success", false);
            model.put("errMsg", e.getMessage());
        }
        if (shop != null && shopImg != null) {
            User user = new User();
            user.setId(1);
            shop.setUser(user);
            Area area = new Area();
            ShopCategory shopCategory = new ShopCategory();
            area.setId(3);
            shopCategory.setId(10L);
            shop.setArea(area);
            shop.setShopCategory(shopCategory);
            ShopExecution se = shopService.addShop(shop, shopImg);
            model.put("success", true);
            model.put("errMsg", "添加商铺店铺成功");
        } else {
            model.put("success", false);
            model.put("errMsg", "商铺店铺信息有误");
            return model;
        }
        return model;
    }

}
